import AdAgent from "./AdAgent";
import { BannerLocation } from "./AdManager";
import PlatUtils from "./PlatUtils";
import { utils } from "./Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class AdAgentOPPO extends AdAgent {

    _bannerAd: any = null;
    _insertAd: any = null;
    _videoAd: any = null;
    _nativeAd: any = null;

    _isBannerShow:boolean = false;

    public Init() {
        if (PlatUtils.IsOPPO && utils.oppoTool && utils.oppoTool.isOverMiniVersion("1031")) {
            cc.log("OPPO 广告代理组件初始化!");
            let self = this;
            qg.initAdService({
                appId: utils.config.oppoconfig.appID,
                isDebug: true,
                success: function (res) {
                    cc.log("OPPO 小游戏广告组件初始化成功!");

                    self._bannerAd = qg.createBannerAd({
                        posId: utils.config.oppoconfig.bannerId[0]
                    });
                    self._bannerAd.onError((err)=>{
                        cc.log("OPPO 小游戏Banner广告出错: ", err.code, err.msg);
                        if(self._isBannerShow){
                            self._bannerAd.show();
                        }
                    });

                    self._insertAd = qg.createInsertAd({
                        posId: utils.config.oppoconfig.insertId
                    });
                    if (self._insertAd) {
                        self._insertAd.onLoad(() => {
                        });
                        self._insertAd.onShow(() => {
                        });
                        self._insertAd.onError((err) => {
                            cc.log("OPPO 小游戏插屏广告出错:",  err.code, err.msg);
                        });

                        self._insertAd.load();
                    }

                    if (utils.oppoTool.isOverMiniVersion("1040")) {
                        self._videoAd = qg.createRewardedVideoAd({
                            posId: utils.config.oppoconfig.videoId
                        });

                        self._videoAd.load();

                        self._videoAd.onError((err) => {
                            cc.log("激励视频出错: ",  err.code, err.msg);
                        });
                    }

                    self._nativeAd = qg.createNativeAd({ 
                        posId: utils.config.oppoconfig.nativeId
                    });
                    self._nativeAd.load();

                },
                fail: function (res) {
                    cc.log("OPPO 小游戏广告组件初始化失败 :", res.code, res.msg);
                },
                complete: function (res) {
                    cc.log("OPPO 小游戏广告组件初始化完成!");
                }
            });




        }
    }

    public ShowBanner(location: BannerLocation) {
        if (PlatUtils.IsOPPO) {
            if (this._bannerAd) {
                this._isBannerShow = true;
                this._bannerAd.show();
            }
        }
    }

    public HideBanner(location: BannerLocation) {
        if (PlatUtils.IsOPPO) {
            if (this._bannerAd) {
                this._isBannerShow = false;
                this._bannerAd.hide();
            }
        }
    }

    public ShowInterstitial(location: BannerLocation = null) {
        if (PlatUtils.IsOPPO) {
            if (this._insertAd) {
                this._insertAd.show();
            }
        }
    }

    public ShowVideo(callback: Function) {
        if (PlatUtils.IsOPPO) {
            if (this._videoAd) {
                this._videoAd.show();
                let closeCallback = callback;
                this._videoAd.onClose((res) => {
                    if (res.isEnded) {
                        cc.log('激励视频广告完成，发放奖励');
                        if (closeCallback) {
                            closeCallback(true);
                        }
                    } else {
                        cc.log('激励视频广告取消关闭，不发放奖励');
                        if (closeCallback) {
                            closeCallback(false);
                        }
                    }
                });
            }
        }
    }

    public GameExit() { }

    public Share() { }
}
